package com.aaa.service.impl;

import com.aaa.dao.DeptDao;
import com.aaa.service.DeptService;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {
	@Autowired
	private DeptDao deptDao;

	@Override
	public List<Map> selectList() {
		return deptDao.selectList();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map batchAdd(MultipartFile file) {
		Map error = new HashMap();
        String errors = "";
        Workbook workbook = null;
        try {
            //利用jar提供的功能，读取文件流到Workbook对象里面
            workbook = Workbook.getWorkbook(file.getInputStream());
        } catch (BiffException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //得到excel中工作簿
        Sheet sheet = workbook.getSheet(0);
        //得到第一个工作簿中一共有多少行
        int rows = sheet.getRows();
        Map map  = null;
        for(int i=1;i<rows;i++){
            try {
                map = new HashMap();
                //根据行下标获取该行数据的列集合
                Cell[] row = sheet.getRow(i);
                //row[0].getContents()  获取单元格内容
                map.put("dname",row[0].getContents());
                map.put("filePath","url");
                map.put("fileName","aaa");
                deptDao.add(map);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                errors+=(i+1)+" ";
            }
        }
        if(!"".equals(errors)){
            error.put("er","错误行号为："+errors);
        }else{
            error.put("er", "导入成功！！！");
        }
        return error;
	}

    @Override
    public int add(Map map) {
        System.out.println(map + "1111111111111111");
        System.out.println(map.get("filePath").toString());
        List s = (List) map.get("filePath");
        Map map1 = new HashMap();
        String s1 = "../img/";
        String s2 = (String) s.get(0);

        String s3=s1+s2;
        System.out.println(s3);
        map1.put("filePath",s3);
        map1.put("title",map.get("title"));
        /* System.out.println(s);
        System.out.println([0].toString());*/
        return deptDao.add(map1);
    }
}
